<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="考试名称" prop="className">
        <el-input v-model="queryParams.examName" placeholder="请输入考试名称" clearable size="small" style="width: 180px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学年学期" prop="status"> </el-form-item>
      <el-select v-model="queryParams.examTerm" placeholder="请选择">
        <el-option lable="请选择" value="">请选择</el-option>
        <el-option v-for="item1 in termTimeList" :key="item1.id" :label="item1.tetiBeginYear + '~' + item1.tetiEndYear"
          :value="item1.id">
          <span style="float: left">{{
            item1.tetiBeginYear + "~" + item1.tetiEndYear
          }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{
            item1.remarks
          }}</span>
        </el-option>
      </el-select>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBatch()"
          :disabled="multiple">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="examList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考试ID" align="center" prop="id" />
      <el-table-column label="考试名称" align="center" prop="examName" :show-overflow-tooltip="true" />
      <el-table-column label="学年学期" align="center" prop="examTerm" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ termTimeLable(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="考试开始日" align="center" prop="examStartDay" width="160"></el-table-column>
      <el-table-column label="考试结束日" align="center" prop="examEndDay" width="160"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" circle type="success" icon="el-icon-edit" @click="handleUpdate(scope.row)" />
          <el-button size="mini" circle type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="考试名称" prop="examName">
          <el-input v-model="form.examName" placeholder="请输入考试名称" />
        </el-form-item>
        <el-form-item label="考试开始日" prop="examStartDay">
          <el-date-picker v-model="form.examStartDay" type="date" value-format="yyyy-MM-dd"
            placeholder="选择开始日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="考试结束日" prop="examEndDay">
          <el-date-picker v-model="form.examEndDay" type="date" value-format="yyyy-MM-dd"
            placeholder="选择开始日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="学年学期">
          <el-select v-model="form.examTerm" placeholder="请选择" style="width: 100%">
            <el-option v-for="item1 in termTimeList" :key="item1.id"
              :label="item1.tetiBeginYear + '~' + item1.tetiEndYear" :value="item1.id">
              <span style="float: left">{{
                item1.tetiBeginYear + "~" + item1.tetiEndYear
              }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item1.remarks
              }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { listTermtime } from "@/api/system/termtime.js";
import { queryPage, add, update, deleteById, getExamPlanById, isOnly } from "@/api/newExam/plan.js";

export default {
  name: "examPlan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      rowList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" },
        ],
        examStartDay: [
          { required: true, message: "考试开始时间不能为空", trigger: "blur" },
        ],
        examEndDay: [
          { required: true, message: "考试结束时间不能为空", trigger: "blur" },
        ],
        examTerm: [
          { required: true, message: "学年学期不能为空", trigger: "blur" },
        ],
      },
      termTimeList: [],
      examList: [],
      ids: ''
    };
  },
  created() {
    this.getTermTimeList();
    this.getList();
  },
  methods: {

    getTermTimeList() {
      //查询学期
      this.loading = true;
      listTermtime({ pageSize: 100, pageNum: 1, status: 1 }).then(response => {
        this.termTimeList = response.data.list;
        console.log("termTime:", response);
        console.log("termTime:", this.termTimeList);
      }
      );
    },

    /** 查询班级列表 */
    getList() {
      this.loading = false;
      queryPage(this.queryParams).then(response => {
        this.examList = response.data.list;
        console.log(response.data.list);
        console.log('=====')
        this.total = response.data.total;
        this.loading = false;
      });
    },
    termTimeLable(row) {
      for (let i = 0; i < this.termTimeList.length; i++) {
        if (this.termTimeList[i].id == row.examTerm) {
          return this.termTimeList[i].remarks;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试信息";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.rowList = selection;

      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.ids = selection.map(temp => temp.id).join("&ids=");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getExamPlanById(row.id).then(resp => {
        this.form = resp.data;
      })
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          isOnly(this.form).then(resp => {
            if (resp.code == '200') {
              if (this.form.id !== undefined) {
                update(this.form).then(response => {
                  if (response.code === 200) {
                    this.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                  } else {
                    this.msgError(response.message);
                  }
                });
              } else {
                add(this.form).then(response => {
                  if (response.code === 200) {
                    this.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                  } else {
                    this.msgError(response.message);
                  }
                });
              }
            } else {
              this.$message({
                message: resp.message,
                type: 'error'
              })
            }
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteById(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },

    handleDeleteBatch() {
      //批量删除
      this.$confirm("是否确认删除选择的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteById(this.ids).then((resp) => {
            this.getList();
            this.msgSuccess("删除成功");
          });
        })
        .catch(function () { });
    },
  },
};
</script>
  